/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.repository;

import dao.ConnectionManager;
import domain.EccezioneUpdater;
import domain.Root;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author Alessio
 */
public class RootRepository {

    private static final String SELECT_ALL_ROOTS_SQL = "SELECT * FROM root";

    public static Collection<Root> findAllRoots() throws EccezioneUpdater {
        Collection<Root> res = new ArrayList<Root>();
        Connection con = null;
        PreparedStatement st = null;
        try {
            con = ConnectionManager.getConnection();
            st = con.prepareStatement(SELECT_ALL_ROOTS_SQL);
            ResultSet rs = st.executeQuery();

            while (rs.next()) {
                Root toAdd = new Root();
                toAdd.setId_root(rs.getInt("id_root"));
                toAdd.setPath(rs.getString("path"));
                res.add(toAdd);
            }
        } catch (ClassNotFoundException ex) {
            throw new EccezioneUpdater("Impossibile caricare il driver per connettersi al database");
        } catch (SQLException ex) {
            throw new EccezioneUpdater(ex.getMessage());
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
                if (st != null) {
                    st.close();
                }
            } catch (SQLException ex) {
            }
        }

        return res;
    }
}